<?php

/**
 * Description of Offer
 *
 * @author Kam
 */
class Application_Model_DbTable_Offer extends My_Db_Table {

    protected $_name = 'offer';
    protected $_primary = 'offer_id';

    /**
     * 
     * @return Zend_Db_Select
     */
    public function selectActiveOffers() {
        $select = $this->select()
                ->from(array('o' => 'offer'))
                ->joinLeft(array('i' => 'image'), 'o.image_id=i.image_id', array('image_code' => 'code', 'image_source_id' => 'source_id', 'image_source_name' => 'source_name', 'image_filename' => 'filename'))
                ->joinLeft(array('c' => 'category'), 'o.category_id=c.category_id', array('category_name' => 'name', 'category_code' => 'code'))
                ->joinLeft(array('cp' => 'category'), 'c.parent_id=cp.category_id', array('parent_category_name' => 'name', 'parent_category_code' => 'code'))
                //@todo aktywne oferty ograniczyć limitem czasowym
                ->order(array(
                    new Zend_Db_Expr('DATE(o.active_from) DESC'),
                    'o.type DESC',
                    'o.weight DESC',
                    'o.active_from DESC',
                ))
        ;
        if (!My_Utils::isAdmin()) {
            $select->where('o.active = 1')
                    ->where('o.admin_active = 1');
        }
        return $select;
    }

    protected function getMainCodeColumnName() {
        return 'code';
    }

}

